#include <bits/stdc++.h>
using namespace std;

int n, a[200005];

namespace CRH380D {
	int dp[10004][10004];
	inline short dfs(int i, int l, int r) {
		if (i == n + 1) return 0;
		if (dp[l][r] != -1) return dp[l][r];
		int ans = 0;
		if (a[i] > l) ans = max(ans, dfs(i + 1, a[i], r) + 1);
		if (a[i] < r) ans = max(ans, dfs(i + 1, l, a[i]) + 1);
		if (a[i] >= l && a[i] <= r) ans = max(ans, (int)dfs(i + 1, l, r));
		return dp[l][r] = ans;
	}
	void main() {
		memset(dp, 255, sizeof(dp));
		cout << dfs(1, 0, n + 1) << "\n";
	}
}

namespace CR200J {
	int dp[204][204][204];
	int dfs(int i, int l, int r) {
		if (i == n + 1) return 0;
		if (dp[i][l][r] != -1) return dp[i][l][r];
		return dp[i][l][r] = max(dfs(i + 1, max(l, a[i]), r) + (a[i] >= l), dfs(i + 1, l, min(r, a[i])) + (a[i] <= r));
	}
	void main() {
		memset(dp, 255, sizeof(dp));
		cout << dfs(1, 1, n) << "\n";
	}
}

signed main() {
	freopen("shung.in", "r", stdin); freopen("shung.out", "w", stdout);
	ios::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
	}
	if (n <= 200) CR200J::main();
	else if (n <= 10000) CRH380D::main();
	return 0;
}
